package com.ruoyi.common.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.domain.TxcheckVO;
import com.ruoyi.common.domain.WxVideoTwoVO;
import com.ruoyi.common.utils.property.WeChatProperty;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class TxCheckUtils {

    //
    private static String url = "https://api.weixin.qq.com/cgi-bin/token";
    private static String appid = SpringUtils.getBean(WeChatProperty.class).getAppId();
    private static String secret = SpringUtils.getBean(WeChatProperty.class).getAppSecret();
    //
    private static String statementUrl = "https://api.weixin.qq.com/cgi-bin/token";
    //
    private static String statementUrlTwo = "https://api.weixin.qq.com/wxa/media_check_async?access_token=";


    public static AjaxResult checkTx(TxcheckVO vo) throws IOException {
        Map<String, Object> get = new HashMap<>();
        get.put("grant_type", "client_credential");
        get.put("appid", appid);
        get.put("secret", secret);
        String s = HttpUtil.get(url, get);
        JSONObject jsonObject1 = JSON.parseObject(s);
        String access_token = jsonObject1.getString("access_token");
        RestTemplate restTemplate = new RestTemplate();
        if ("1".equals(vo.getIsFlag())) {
            //创建客户端
            String postUrl = statementUrl + access_token;
            HashMap<String, Object> data = new HashMap<>();
            data.put("version", 2);
            data.put("openid", vo.getOpenId());
            data.put("scene", 1);
            data.put("content", vo.getContent());
            byte[] bytes = vo.getContent().getBytes("UTF-8");
            String decodedStr = new String(bytes, StandardCharsets.UTF_8);
            data.put("content", decodedStr);
            String results = restTemplate.postForObject(postUrl, data, String.class);
            if (StringUtils.isNotEmpty(results)) {
                Gson gson = new Gson();
                WxVideoTwoVO wxVideoTwoVO = gson.fromJson(results, WxVideoTwoVO.class);
                Object result = wxVideoTwoVO.getResult();
                JSONObject from = JSONObject.from(result);
                Object suggest = from.get("suggest");
                Object label = from.get("label");
                if ("pass".equals(suggest.toString()) & "100.0".equals(label.toString())) {
                    return AjaxResult.success(results);
                }
                return AjaxResult.error(results);
            }
        } else {
            String postUrlTwo = statementUrlTwo + access_token;
            HashMap<String, Object> dataTwo = new HashMap<>();
            dataTwo.put("version", 2);
            dataTwo.put("openid", vo.getOpenId());
            dataTwo.put("scene", 1);
            dataTwo.put("media_url", vo.getUrl());
            dataTwo.put("media_type", vo.getMediaType());
            String Two = restTemplate.postForObject(postUrlTwo, dataTwo, String.class);
            return AjaxResult.success(Two);
        }
        return null;
    }
}